package com.ruibang.glass.portal.controller;

import com.ruibang.glass.common.domain.SuperviseNotice;
import com.ruibang.glass.portal.domain.NoticeReq;
import com.ruibang.glass.portal.entity.Notice;
import com.ruibang.glass.portal.service.NoticeService;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 通知公告 前端控制器
 * </p>
 *
 * @author 此代码为自动生成
 * @since 2019-11-12
 */
@Api(tags = "通知公告接口")
@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Autowired
    private NoticeService service;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取详情", response = ResultBody.class)
    public ResultBody<Notice> get(@PathVariable String id) {
        return ResultBody.success(service.getById(id));
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增实体", response = ResultBody.class)
    public ResultBody add(@RequestBody @ApiParam("实体信息") Notice entity) {
        LocalDateTime now = LocalDateTime.now();
        if (entity.getCreateTime() == null) entity.setCreateTime(now);
        if (entity.getUpdateTime() == null) entity.setUpdateTime(now);
        service.save(entity);
        return ResultBody.success();
    }

    @PostMapping("/batchAdd")
    @ApiOperation(value = "批量新增实体", response = ResultBody.class)
    public ResultBody batchAdd(@RequestBody @ApiParam("实体信息") List<Notice> entityList) {
        service.saveBatch(entityList);
        return ResultBody.success();
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改实体", response = ResultBody.class)
    public ResultBody update(@RequestBody Notice entity) {
        entity.setUpdateTime(LocalDateTime.now());
        service.updateById(entity);
        return ResultBody.success();
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除实体", response = ResultBody.class)
    public ResultBody delete(@PathVariable @ApiParam("主键ID") Long id) {
        service.removeById(id);
        return ResultBody.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "批量删除实体", response = ResultBody.class)
    public ResultBody delete(@RequestBody @ApiParam("主键ID") List<Serializable> ids) {
        service.removeByIds(ids);
        return ResultBody.success();
    }

    @PostMapping("/queryNotices")
    @ApiOperation(value = "根据条件查询通知信息", response = ResultBody.class)
    public ResultBody<PageResult<List<Notice>>> queryNotices(@RequestBody @ApiParam("查询条件") NoticeReq noticeReq) {
        return service.queryNotices(noticeReq);
    }

    @PostMapping("/queryNoticeByBusinessIds")
    @ApiOperation(value = "根据业务Ids查询通知信息", response = ResultBody.class)
    public ResultBody<List<SuperviseNotice>> queryNoticeByBusinessIds(@RequestBody List<String> ids) {
        return ResultBody.success(service.queryNoticeByBusinessIds(ids));
    }


    @PostMapping("/queryHistoryNotices")
    @ApiOperation(value = "查询历史督办消息", response = ResultBody.class)
    public ResultBody<List<Notice>> queryHistoryNotices(@RequestBody @ApiParam("查询条件") NoticeReq noticeReq) {
        return service.queryHistoryNotices(noticeReq);
    }

    @GetMapping("/getSuperviseTimes")
    @ApiOperation(value = "获取催办次数", response = ResultBody.class)
    public Integer getSuperviseTimes(@RequestParam("businessId") String businessId) {
        return service.getSuperviseTimes(businessId);
    }

}
